import 'package:flutter/material.dart';
import 'package:ichinesereader/routes/routePages.dart';
import 'package:ichinesereader/themes/pageStyles.dart';
import 'package:sidebarx/sidebarx.dart';
import 'package:ichinesereader/utils/log.dart';
import 'package:get/get.dart';

String currentSelectedPageRoute = '/student/audiobooks';

class StudentSidebarX extends StatefulWidget {
  const StudentSidebarX(
      {Key? key, required this.selectedIndex, required this.isBigScreen})
      : super(key: key);
  final int selectedIndex;
  final bool isBigScreen;

  @override
  State<StudentSidebarX> createState() => _StudentSidebarXState();
}

class _StudentSidebarXState extends State<StudentSidebarX> {
  final SidebarXController controller =
      SidebarXController(selectedIndex: 0, extended: false);
  final PageStyles _pageStyles = PageStyles();

  @override
  void initState() {
    super.initState();
    controller.selectIndex(widget.selectedIndex);
    if (widget.isBigScreen) {
      controller.setExtended(true);
    }
  }

  @override
  Widget build(BuildContext context) {
    Log.debug('start building StudentSidebarX');

    return Container(
      margin: _pageStyles.sideBarPageEdgeInsets(),
      decoration: _pageStyles.pageSidebarDecoration(),
      child: SidebarX(
        controller: controller,
        theme: _pageStyles.pageSidebarXTheme(),
        extendedTheme: _pageStyles.pageSidebarXExtendTheme(),
        headerBuilder: (context, extended) {
          return Container(
            child: Container(
              margin: EdgeInsets.all(3.0),
              padding: EdgeInsets.all(6.0),
              decoration: BoxDecoration(
                  color: Get.isDarkMode
                      ? Colors.white.withOpacity(0.1)
                      : Colors.black.withOpacity(0.1),
                  shape: BoxShape.circle),
              child: GestureDetector(
                  onTap: () {
                    Get.offAllNamed('/student/home');
                  },
                  child: Image.asset('assets/images/brand.png')),
            ),
          );
        },
        //lists 之间没有调整间距的选项
        items: RoutePages.studentPageItems
            .map((page) => SidebarXItem(
                  iconWidget: Tooltip(
                    textStyle: TextStyle(
                        color: Get.isDarkMode
                            ? Get.theme.primaryColor
                            : Get.theme.primaryColorLight),
                    message: page.name.tr,
                    child: Icon(page.icon,
                        color: Get.isDarkMode
                            ? Get.theme.primaryColorDark
                            : Get.theme.primaryColorLight),
                  ),
                  // icon: page.icon,
                  label: page.name.tr,
                  onTap: () {
                    if (page.name == 'Home') {
                      Get.offAllNamed(page.route);
                    } else {
                      if (GetPlatform.isWeb) {
                        if (currentSelectedPageRoute == '/student/audiobooks') {
                          currentSelectedPageRoute = page.route;
                          Get.offAllNamed(page.route);
                        } else {
                          currentSelectedPageRoute = page.route;
                          Get.toNamed(page.route);
                        }
                      } else {
                        Get.offAllNamed(page.route);
                      }
                    }
                  },
                ))
            .toList(),
      ),
    );
  }
}
